﻿using System;
using System.Data;
using System.Data.Common;

namespace StampController.Core
{
    public static class DataUtils
    {
        public static TSource Get<TSource>(this DbDataReader reader, int index)
        {
            return reader.IsDBNull(index) ? default(TSource) : (TSource) reader.GetValue(index);
        }

        public static TSource Get<TSource>(this DbDataReader reader, int index, object defaultValue)
        {
            return (TSource) (reader.IsDBNull(index) ? defaultValue : reader.GetValue(index));
        }

        public static string GetEmpty(this DbDataReader reader, int index)
        {
            return reader.IsDBNull(index) ? string.Empty : reader.GetString(index);
        }

        public static TSource Cells<TSource>(this DataRow row, string index)
        {
            return row[index] == DBNull.Value ? default(TSource) : (TSource) row[index];
        }
    }
}